﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MaterialQuotaSetting.ascx.cs" 
    Inherits="DMS.Web.Common.Controls.MaterialQuotaSetting" %>
<asp:HiddenField ID="hidConfigID" runat="server" />
<asp:HiddenField ID="hidMemberID" runat="server" />
<asp:HiddenField ID="hidMemberType" runat="server" />
<table cellpadding="1" cellspacing="2" border="0" width="100%">
    <tr>
        <td width="160px" valign="top">
            <asp:Label ID="lblConfigTitle" runat="server">参数名称</asp:Label>：
        </td>
        <td>
            <nobr>
                选择物资&nbsp;<input type="text" id="txtMaterialName" readonly title="请点击右侧按钮选择物资"  />
                <input type="hidden" id="hidMaterialID" />
                <img src="/Resources/Images/icon_detail.gif" style="cursor:hand;" onclick="javascript:SelectMaterial(this);" />
                ，数量&nbsp;<input type="text" id="txtMQuota" size="5" width="50" value="0" />
                <a href="javascript:" onclick="javascript:AddMaterialToList(this);">添加</a>
            </nobr>
            <div style="padding-top:3px;">
                <asp:ListBox ID="lbMConifgDetail" runat="server" Rows="10" Width="350px"></asp:ListBox><br />
                <asp:HiddenField ID="hidSelectedItem" runat="server" />
                <table cellpadding="0" cellspacing="0" border="0" width="350px">
                    <tr>
                        <td>提示：双击删除限制物资</td>
                        <td width="100px" align="right">
                        <a href="javascript:" onclick="javascript:if(window.confirm('确认删除所有限制？')){Clear();};">删除所有限制</a>
                        </td>
                    </tr>
                </table>
            </div>
        </td>
    </tr>
</table>
<script language="javascript" type="text/javascript">
<!--
    function SelectMaterial(obj) {
        var txtMaterialName = document.getElementById("txtMaterialName");
        var hidMaterialID = document.getElementById("hidMaterialID");
        var res = OpenMaterialSelector(obj, "SingleSelector", "");
//        alert(res);

        if (res != null || res == "") {
            var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.loadXML("<Materials>" + res + "</Materials>");
            if (xmlDoc.parseError.errorCode != 0) {
                alert("选择物资信息后，返回的数据有异常，请联系管理员。");
                return;
            }
            var doc = xmlDoc.documentElement;
            if (doc.childNodes.length > 0) {
                var materialInfo = doc.childNodes[0];
                txtMaterialName.value = materialInfo.getAttribute("Name");
                hidMaterialID.value = materialInfo.getAttribute("ID");
            }
        }
    }
    function AddMaterialToList(obj) {
        var txtMaterialName = document.getElementById("txtMaterialName");
        var hidMaterialID = document.getElementById("hidMaterialID");
        var txtMQuota = document.getElementById("txtMQuota");

        if (hidMaterialID.value == "") {
            alert("请选择要限制的物资！");
            SelectMaterial(null);
            return;
        }
        if (isNaN(txtMQuota.value)) {
            alert("请输入正确的数量限制！");
            txtMQuota.focus();
            return;
        }
        if (txtMQuota.value < 0) {
            alert("数量限制不能少于0，请重新输入！");
            txtMQuota.focus();
            return;
        }

        var selector = document.getElementById("<%=lbMConifgDetail.ClientID %>");
        var textStr = "物资：" + txtMaterialName.value + "，数量限额：" + txtMQuota.value;
        var valueStr = hidMaterialID.value + ":" + txtMQuota.value;

        for (var index = 0; index < selector.options.length; index++) {
            if (selector.options[index].value.indexOf(hidMaterialID.value) >= 0) {
                selector.remove(index);
                break;
            }
        }

        var item = document.createElement("OPTION");
        item.text = textStr;
        item.value = valueStr;

        selector.add(item, 0);
        updateSelectorValue();
        //ResetInputer();
    }

    function RemoveItem(selector) {
        if (selector == null || selector.selectedIndex == -1) {
            return;
        }

        selector.remove(selector.selectedIndex);
        updateSelectorValue();
    }

    function Clear() {
        var selector = document.getElementById("<%=lbMConifgDetail.ClientID %>");
        while (selector.options.length > 0) {
            selector.remove(0);
        }

        updateSelectorValue();
    }

    function ResetInputer() {
        var txtMaterialName = document.getElementById("txtMaterialName");
        var hidMaterialID = document.getElementById("hidMaterialID");
        var txtMQuota = document.getElementById("txtMQuota");

        txtMaterialName.value = "";
        hidMaterialID.value = "";
        //txtMQuota.value = 0;

        txtMaterialName.focus();
    }

    function updateSelectorValue() {
        var selector = document.getElementById("<%=lbMConifgDetail.ClientID %>");
        var valueHolder = document.getElementById("<%=hidSelectedItem.ClientID %>");

        var valStr = "";
        for (var index = 0; index < selector.options.length; index++) {
            valStr += (selector.options[index].value + ";");
        }

        valueHolder.value = valStr;
    }
//-->
</script>
